import java.util.HashMap;
import java.util.Map;

public class Solution6 {
    //974. 和可被 K 整除的子数组
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,1);
        int ret = 0,sum = 0;
        for (int num : nums) {
            sum += num;
            int i = (sum % k + k) % k;
            ret += hash.getOrDefault(i,0);
            hash.put(i,hash.getOrDefault(i,0)+1);
        }
        return ret;
    }
}
